package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.config.AccPushProccesor;
import com.example.prison.config.ApiUtil;
import com.example.prison.entity.AttendanceDeviceInfo;
import com.example.prison.mapper.AttendanceDeviceInfoMapper;
import com.example.prison.service.AttendanceDeviceInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
* @author jiang
* @description 针对表【attendance_device_info】的数据库操作Service实现
* @createDate 2023-04-19 11:30:58
*/
@Service
public class AttendanceDeviceInfoServiceImpl extends ServiceImpl<AttendanceDeviceInfoMapper, AttendanceDeviceInfo>
    implements AttendanceDeviceInfoService {

    @Autowired
    private AttendanceDeviceInfoMapper attendanceDeviceInfoMapper;

    @Override
    public PageInfo<AttendanceDeviceInfo> get(int pageNumber, int pageSize, String deviceNumber, String deviceName) {
        List<AttendanceDeviceInfo> list = attendanceDeviceInfoMapper.get(deviceNumber,deviceName);
        String time = ApiUtil.timeProcess(-2);
        for(AttendanceDeviceInfo device : list) {
//            String status = device.getDeviceStatus();
//            if(AccPushProccesor.number == null || AccPushProccesor.time.compareTo(time) < 0) {
//                status = "离线";
//            } else if(Objects.equals(device.getDeviceNumber(), AccPushProccesor.number) && AccPushProccesor.time.compareTo(time) > 0) {
//                status = "在线";
//            }
            String status = device.getDeviceStatus();
            status = ApiUtil.getDeviceStatus(device.getDeviceNumber());
            device.setDeviceStatus(status);
            attendanceDeviceInfoMapper.updateAttendanceDevice(device, device.getDeviceId());
        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(attendanceDeviceInfoMapper.get(deviceNumber,deviceName));
    }

    @Override
    public int add(AttendanceDeviceInfo attendanceDevice) {
        String deviceNumber = attendanceDevice.getDeviceNumber();
        if (ObjectUtils.isNotEmpty(attendanceDeviceInfoMapper.getOneByNumber(deviceNumber))) {
            return -1;
        } else {
            attendanceDevice.setDeviceStatus("离线");
            attendanceDeviceInfoMapper.add(attendanceDevice);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        attendanceDeviceInfoMapper.deleteAttendanceDevice(ids);
    }

    @Override
    public int update(AttendanceDeviceInfo attendanceDevice, Integer deviceId) {
        if(attendanceDeviceInfoMapper.getListByNumber(attendanceDevice.getDeviceNumber()).size() > 1) {
            return -1;
        } else {
            attendanceDeviceInfoMapper.updateAttendanceDevice(attendanceDevice, deviceId);
            return 1;
        }
    }

    @Override
    public AttendanceDeviceInfo getById(Integer deviceId) {
        return attendanceDeviceInfoMapper.getById(deviceId);
    }

    @Override
    public PageInfo<AttendanceDeviceInfo> getByName(int pageNumber, int pageSize, String deviceName) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(attendanceDeviceInfoMapper.getByName(deviceName));
    }

    @Override
    public PageInfo<AttendanceDeviceInfo> getByNumber(int pageNumber, int pageSize, String deviceNumber) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(attendanceDeviceInfoMapper.getByNumber(deviceNumber));
    }

    @Override
    public AttendanceDeviceInfo getOneByNumber(String deviceNumber) {
        return attendanceDeviceInfoMapper.getOneByNumber(deviceNumber);
    }

    @Override
    public AttendanceDeviceInfo getOneByNumberAndStatus(String deviceNumber,String status) {
        return attendanceDeviceInfoMapper.getOneByNumberAndStatus(deviceNumber,status);
    }

    @Override
    public PageInfo<AttendanceDeviceInfo> getRealStatus(int pageNumber, int pageSize) {
        List<AttendanceDeviceInfo> list = attendanceDeviceInfoMapper.get("","");
        String time = ApiUtil.timeProcess(-2);
        for(AttendanceDeviceInfo device : list) {
            String status = device.getDeviceStatus();
            if(Objects.equals(device.getDeviceNumber(), AccPushProccesor.number) && AccPushProccesor.time.compareTo(time) > 0) {
                status = "在线";
            }
            device.setDeviceStatus(status);
            attendanceDeviceInfoMapper.updateAttendanceDevice(device, device.getDeviceId());
        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(attendanceDeviceInfoMapper.get("",""));
    }


}




